package com.woshengqile.service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.hasor.core.Init;
import net.hasor.core.Singleton;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Predicate;

import com.woshengqile.plugin.StoragePlugin;
import com.woshengqile.plugin.ftpStorage.FtpStoragePlugin;
import com.woshengqile.plugin.localStorage.LocalStoragePlugin;
import com.woshengqile.plugin.ossStorage.OssStoragePlugin;

/**
 * Service - 插件
 * 
 */
@Singleton
public class PluginService {
	
	// 存储插件
	private List<StoragePlugin> storagePlugins = new ArrayList<>();
	// 存储插件
	private Map<String, StoragePlugin> storagePluginMap = new HashMap<>();
	
	/**
	 * 构造方法
	 */
	@Init
	public void init() {
		// FTP存储
		FtpStoragePlugin ftpStoragePlugin = new FtpStoragePlugin();
				
		// 本地文件存储
		LocalStoragePlugin localStoragePlugin = new LocalStoragePlugin();
		
		// 阿里云存储
		OssStoragePlugin ossStoragePlugin = new OssStoragePlugin();
		
		/*****  存储插件  ******/
		storagePlugins.add(ossStoragePlugin);
		storagePlugins.add(localStoragePlugin);
		storagePlugins.add(ftpStoragePlugin);
		
		storagePluginMap.put(ossStoragePlugin.getId(), ossStoragePlugin);
		storagePluginMap.put(localStoragePlugin.getId(), localStoragePlugin);
		storagePluginMap.put(localStoragePlugin.getId(), localStoragePlugin);
	}
	
	/**
	 * 获取存储插件
	 * 
	 * @return 存储插件
	 */
	public List<StoragePlugin> getStoragePlugins() {
		Collections.sort(storagePlugins);
		return storagePlugins;
	}

	/**
	 * 获取存储插件
	 * 
	 * @param isEnabled
	 *            是否启用
	 * @return 存储插件
	 */
	public List<StoragePlugin> getStoragePlugins(final boolean isEnabled) {
		List<StoragePlugin> result = new ArrayList<>();
		CollectionUtils.select(storagePlugins, new Predicate() {
			@Override
			public boolean evaluate(Object object) {
				StoragePlugin storagePlugin = (StoragePlugin) object;
				return storagePlugin.getIsEnabled() == isEnabled;
			}
		}, result);
		Collections.sort(result);
		return result;
	}

	/**
	 * 获取存储插件
	 * 
	 * @param id
	 *            ID
	 * @return 存储插件
	 */
	public StoragePlugin getStoragePlugin(String id) {
		return storagePluginMap.get(id);
	}

}